/*
	CVAR:
	nes_bank	(1)
		1/0 -	Enables/disables bank

	Commands:
		add_to_bank [money] - adds money to bank
		take_from_bank [money] - takes money from bank
		say /bankmenu - Shows bank menu
			*bank menu is also available from main menu (command say /menu)
		
*/


#if !defined NES_GLOBALS_INCLUDED
#include "include/nes/nes_globals.inl"
#endif

#if !defined NES_SAVE_INCLUDED
#include "include/nes/nes_save.inl"
#endif

#define NES_BANK_INCLUDED 1

#if defined USE_SQL
new gDbTupleBank
#else
new gVaultBank;
#endif

new gCvarBankEnabled;

enum BANK_MENU_ITEM{
	BMI_ADD = 1,
	BMI_TAKE
}

public plugin_init_bank() {
	register_concmd("add_to_bank", "comAddToBank");
	register_concmd("take_from_bank", "comTakeFromBank");
	register_concmd("say /bankmenu", "showMenuBank");
	gCvarBankEnabled = register_cvar("nes_bank", "1");
	
	#if defined USE_SQL
	gDbTupleBank = 0;
	#else
	gVaultBank = nvault_open("nes_bank");
	#endif
}
stock isBankEnabled(){
	return get_pcvar_num(gCvarBankEnabled);
}

public comAddToBank(id, level, cid){
	if(!isActive())
		return PLUGIN_CONTINUE;
	if(!isBankEnabled())
		return PLUGIN_CONTINUE;
	if(cmd_access(id, level, cid, 1)) {
		new moneyS[16];
		read_argv(1, moneyS, charsmax(moneyS));
		remove_quotes(moneyS);
		replace_all(moneyS, charsmax(moneyS), "-","");
		if(!is_str_num(moneyS)){
			client_print(id, print_console, "%L", LANG_PLAYER, "INVALID_NUMBER");
		} else {
			addMoneyToBank(id, str_to_num(moneyS));
		}
	}
	return PLUGIN_HANDLED;
}

public comTakeFromBank(id, level, cid){
	if(!isActive())
		return PLUGIN_CONTINUE;
	if(!isBankEnabled())
		return PLUGIN_CONTINUE;
	if(cmd_access(id, level, cid, 1)) {
		new moneyS[16];
		read_argv(1, moneyS, charsmax(moneyS));
		remove_quotes(moneyS);
		replace_all(moneyS, charsmax(moneyS), "-","");
		if(!is_str_num(moneyS)){
			client_print(id, print_console, "%L", LANG_PLAYER, "INVALID_NUMBER");
		} else {
			getMoneyFromBank(id, str_to_num(moneyS));
		}
	}
	return PLUGIN_HANDLED;
}

public showMenuBank(id){
	if(!isActive())
		return PLUGIN_CONTINUE;
	if(!isBankEnabled())
		return PLUGIN_CONTINUE;
		
	new title[64];
	formatex(title, charsmax(title), "%L", id, "TITLE_BANK");
	
	new menu = menu_create(title, "menuBankHandler");
	new size = getBankMoney(id);
	new money = cs_get_user_money(id);
	new itemInfo[2];
	
	formatex(title, charsmax(title), "%L", id, "BANK_ACCONUT_SIZE",  size);
	menu_addtext(menu, title);
	
	if(money > 0){
		formatex(title, charsmax(title), "%L", id, "BANK_ACCONUT_ADD");
		itemInfo[0] = BMI_ADD;
		menu_additem(menu, title, itemInfo);
	}
	if(size > 0){
		formatex(title, charsmax(title), "%L", id, "BANK_ACCONUT_TAKE");
		itemInfo[0] = BMI_TAKE;
		menu_additem(menu, title, itemInfo);
	}
	menu_display(id, menu);
}

public menuBankHandler(id, menu, item){
	
}

stock getBankMoney(id){
	new data[16];
	if(nvault_lookup(gVaultBank, playerKey[id], data, charsmax(data), timestamp)){
		return strtonum(data);
	}
	return 0;
}

stock getMoneyFromBank(id, money){
	new oldBankMoney = getBankMoney(id);
	if(oldBankMoney >= money){
		new userMoney = cs_get_user_money(id);
		cs_set_user_money(id, userMoney + money);
		saveUserMoney(id, oldBankMoney - money);
		client_print(id, print_chat, "%L", LANG_PLAYER, "BANK_MONEY_TAKEN", money, oldBankMoney - money);
	} else {
		client_print(id, print_chat, "%L", LANG_PLAYER, "BANK_TOO_LOW_MONEY", money, oldBankMoney);
	}
}

stock saveUserMoney(id, money){
	new mStr[16];
	num_to_str(money, mStr, charsmax(mStr));
	nvault_set(gVaultBank, playerKey[id], mStr);
}

stock addMoneyToBank(id, money){
	new userMoney = cs_get_user_money(id);
	if(userMoney >= money){
		new oldBankMoney = getBankMoney(id);		
		cs_set_user_money(id, userMoney - money);
		saveUserMoney(id, oldBankMoney + money);
		
		client_print(id, print_chat, "%L", LANG_PLAYER, "BANK_MONEY_ADDED", money, oldBankMoney + money);
	} else {
		client_print(id, print_chat, "%L", LANG_PLAYER, "BANK_NOT_ENOUGH_MONEY", money, userMoney);
	}
}
